from multiprocessing.dummy import Pool

from lxml import etree

from apps.chemblink.chemblink import save_pic
from core.db_utils import MysqlManager
from core.http_client import HttpClient
from core.logger import root_logger

db_manager = MysqlManager()
http_client = HttpClient()


def get_urls():
    sql = 'select url from chemblink_url_more where got=0'
    return db_manager.fetch_data(sql)


def get_info(url):
    prod_url = url[0]
    print(prod_url)
    try:
        resp = http_client.get(prod_url)
        response = resp.content.decode('utf-8', errors="ignore")
        html = etree.HTML(response)
        trs = html.xpath('//table[@class="right"]/tr')
        en_name = ''
        img_url = ''
        bie_ming_name = ''
        fenzishi = ''
        fenziliang = ''
        cas_login_num = ''
        einecs_login_num = ''
        SMILES = ''
        InChI = ''
        InChIKey = ''
        for tr in trs:
            key_name_ele = tr.xpath('th[1]/text()')
            if not key_name_ele:
                key_name_ele = tr.xpath('td[1]/text()')
            key_name = ''.join(key_name_ele)
            if key_name == "产品名称":
                prod_name_ele = tr.xpath('th[2]/text()')
                if not prod_name_ele:
                    prod_name_ele = tr.xpath('td[2]/text()')
                prod_name = ''.join(prod_name_ele)
            elif key_name == "英文名":
                en_name_ele = tr.xpath('td[2]/text()')
                en_name = ''.join(en_name_ele)
            elif key_name == "分子结构":
                img_url_ele = tr.xpath('td[2]/img/@src')
                img_url = ''.join(img_url_ele)
                if img_url != '':
                    img_url = 'https://www.chemblink.com' + img_url
            elif key_name == "别名":
                bie_ming_name_ele = tr.xpath('td[2]/text()')
                bie_ming_name = ''.join(bie_ming_name_ele)
            elif key_name == "分子式":
                fzs_list = tr.xpath('td[2]//text()')
                fenzishi = ''.join(fzs_list)
            elif key_name == "分子量":
                fenziliang_ele = tr.xpath('td[2]/text()')
                fenziliang = ''.join(fenziliang_ele)
            elif key_name == "CAS 登录号":
                cas_login_num_ele = tr.xpath('td[2]/text()')
                cas_login_num = ''.join(cas_login_num_ele)
            elif key_name == "EINECS 登录号":
                einecs_login_num_ele = tr.xpath('td[2]/text()')
                einecs_login_num = ''.join(einecs_login_num_ele)
            elif key_name == "分子行输入简码 SMILES":
                SMILES_ele = tr.xpath('td[2]/text()')
                SMILES = ''.join(SMILES_ele)
            elif key_name == "国际化学标识码 InChI":
                InChI_ele = tr.xpath('td[2]/text()')
                InChI = ''.join(InChI_ele)
            elif key_name == "国际化学标识检索码 InChIKey":
                InChIKey_ele = tr.xpath('td[2]/text()')
                InChIKey = ''.join(InChIKey_ele)

        if img_url != '' and img_url.endswith('.gif'):
            save_pic(url=img_url, name=cas_login_num)

        sql = 'insert into chemblink_more (prod_name, en_name, bie_ming_name, fenzishi, fenziliang, cas_login_num, einecs_login_num, SMILES, InChI, InChIKey,img_url) values ("{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}")'.format(
            prod_name, en_name, bie_ming_name, fenzishi, fenziliang, cas_login_num, einecs_login_num, SMILES, InChI,
            InChIKey, img_url)
        db_manager.execute_sql(sql)

        sql = 'update chemblink_url_more set got=1 where url=%s'
        db_manager.execute_sql(sql, (url,))
    except Exception as e:
        root_logger.error(f'爬取异常：{e}，链接:{prod_url}')
        sql = 'insert into chemblink_more_exc (url, e) values("{}","{}")'.format(prod_url, str(e))
        db_manager.execute_sql(sql)
        # print(prod_name, en_name, bie_ming_name, img_url, fenzishi, fenziliang, cas_login_num, einecs_login_num, SMILES,
        #       InChI, InChIKey)


def get_prod():
    urls = get_urls()
    pool = Pool(15)
    pool.map(get_info, urls)
    pool.close()
    pool.join()


def task(item):
    cas_login_num = item[0]
    url = f'https://www.chemblink.com/moreProducts/more{cas_login_num.strip()}C.htm'
    print(url)
    sql = 'update chemblink_url_more set got=1 where url=%s'
    db_manager.execute_sql(sql, (url,))


def wash():
    sql = 'select cas_login_num from chemblink_more'
    result = db_manager.fetch_data(sql)
    pool = Pool(50)
    pool.map(task, result)
    pool.close()
    pool.join()
